/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.una.scece.clases;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

/**
 *
 * @author Erick
 */
public class Correo {

    final String correo = "erickperez091@gmail.com";
    final String contrasenia = "";
    final String servidorSMTP = "smtp.gmail.com";
    final String puertoEnvio = "465";
    Properties props;

    public Correo() {
        props = new Properties();
        props.put("mail.smtp.user", correo);
        props.put("mail.smtp.host", servidorSMTP);
        props.put("mail.smtp.port", puertoEnvio);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", puertoEnvio);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
    }

    public void enviarCorreo(String destino, String asunto, String mensaje) throws Exception {
        Authenticator auth = new autentificadorSMTP();
        Session session = Session.getInstance(props, auth);
        MimeMessage msg = new MimeMessage(session);
        msg.setText(mensaje);
        msg.setSubject(asunto);
        msg.setFrom(new InternetAddress(correo));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(destino));
        Transport.send(msg);
    }

    private class autentificadorSMTP extends javax.mail.Authenticator {

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(correo, contrasenia);
        }
    }
}
